package com.ly.demo.filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class AuthorizationFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest HttpRequest = (HttpServletRequest) servletRequest;

        if (HttpRequest.getSession().getAttribute("loginUser") != null) {
            filterChain.doFilter(servletRequest, servletResponse);
        }

        HttpServletResponse httpResponse = (HttpServletResponse) servletResponse;

        httpResponse.setContentType("text/html;charset=UTF-8");

        PrintWriter out = httpResponse.getWriter();
        out.print("你未登录");
        //out.print("<script>alert('请先进行登陆，再进行后续操作！(Filter控制)');location.href='" + HttpRequest.getContextPath() + "/login.jsp';</script>");
        out.flush();
        out.close();
    }
}
